package org.shan.hangtian.ui;

import org.shan.hangtian.tp.RunMode;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ModeDialog extends JDialog {
    private JButton mode1, mode2, mode3;

    private CenterPanel centerPanel;

    public ModeDialog(CenterPanel centerPanel) {
        this.centerPanel = centerPanel;
        setBounds((MainFrame.SCREEN_W - 400) / 2, (MainFrame.SCREEN_H - 300) / 2, 400, 300);
        setLayout(new GridLayout(3, 1));

        RunMode[] modes = RunMode.values();
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 20));
        {
            mode1 = new JButton(modes[0].toString());
            mode1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            mode1.setBackground(new Color(69, 142, 253));
            mode1.setForeground(new Color(255, 255, 255));
            mode1.setFont(new Font("黑体", Font.PLAIN, 16));
            mode1.setFocusPainted(false);
            panel.add(mode1);
        }
        add(panel);

        panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 10));
        {
            mode2 = new JButton(modes[1].toString());
            mode2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            mode2.setBackground(new Color(69, 142, 253));
            mode2.setForeground(new Color(255, 255, 255));
            mode2.setFont(new Font("黑体", Font.PLAIN, 16));
            mode2.setFocusPainted(false);
            panel.add(mode2);
        }
        add(panel);

        panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 10));
        {
            mode3 = new JButton(modes[2].toString());
            mode3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            mode3.setBackground(new Color(69, 142, 253));
            mode3.setForeground(new Color(255, 255, 255));
            mode3.setFont(new Font("黑体", Font.PLAIN, 16));
            mode3.setFocusPainted(false);
            panel.add(mode3);
        }
        add(panel);

        addListeners();
    }

    private void addListeners() {
        mode1.addActionListener(new MyActionListener(RunMode.MODE_1));
        mode2.addActionListener(new MyActionListener(RunMode.MODE_2));
        mode3.addActionListener(new MyActionListener(RunMode.MODE_OTHER));
    }

    class MyActionListener implements ActionListener {
        private RunMode mode;

        public MyActionListener(RunMode mode) {
            this.mode = mode;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            ModeDialog.this.dispose();
            centerPanel.changeMode(mode);
        }
    }
}
